home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 June / PCWorld_2007-06_cd.bin / v cisle / tclock / tclocklight-040702-3.exe / source / common / alarmstruct.c next >
C/C++ Source or Header  |  2004-03-06  |  5KB  |  221 lines

  1. /*-------------------------------------------------------------
  2.   alarmstruct.c : load and save ALARMSTRUCT
  3.   (C) 1997-2003 Kazuto Sato
  4.   Please read readme.txt about the license.
  5.   
  6.   Written by Kazubon, Nanashi-san
  7. ---------------------------------------------------------------*/
  8.  
  9. #include "common.h"
  10.  
  11. /* Globals */
  12.  
  13. void LoadAlarm(PALARMSTRUCT pAS, int count);
  14. void SaveAlarm(const PALARMSTRUCT pAS, int count);
  15. void SetAlarmTime(PALARMSTRUCT pAS);
  16.  
  17. /* Statics */
  18.  
  19. static void ParseAlarmFormat(char* dst, const char* src, int first, int last);
  20. static void ParseAlarmFormatSub(int *n1, int *n2, int *n3, BOOL *asta,
  21.     const char* part);
  22.  
  23. /*------------------------------------------------
  24.   read settings of Alarm
  25. --------------------------------------------------*/
  26. void LoadAlarm(PALARMSTRUCT pAS, int count)
  27. {
  28.     int i;
  29.     char subkey[20];
  30.     
  31.     for(i = 0; i < count; i++)
  32.     {
  33.         wsprintf(subkey, "Alarm%d", i + 1);
  34.         
  35.         memset(pAS + i, 0, sizeof(ALARMSTRUCT));
  36.         
  37.         GetMyRegStr(subkey, "Name", pAS[i].name, BUFSIZE_NAME, "");
  38.         pAS[i].bEnable = GetMyRegLong(subkey, "Alarm", FALSE);
  39.         
  40.         GetMyRegStr(subkey, "Hour", pAS[i].strHours, 80, "");
  41.         GetMyRegStr(subkey, "Minute", pAS[i].strMinutes, 80, "");
  42.         GetMyRegStr(subkey, "WDays", pAS[i].strWDays, 80, "");
  43.         
  44.         pAS[i].second = GetMyRegLong(subkey, "Second", 0);
  45.         
  46.         GetMyRegStr(subkey, "File", pAS[i].fname, MAX_PATH, "");
  47.         pAS[i].bHour12 = GetMyRegLong(subkey, "Hour12", TRUE);
  48.         pAS[i].bRepeat = GetMyRegLong(subkey, "Repeat", FALSE);
  49.         pAS[i].bBlink = GetMyRegLong(subkey, "Blink", FALSE);
  50.         pAS[i].bBootExec = GetMyRegLong(subkey, "OnBoot", FALSE);
  51.         pAS[i].bInterval = GetMyRegLong(subkey, "Interval", FALSE);
  52.         pAS[i].nInterval = GetMyRegLong(subkey, "IntervalMinutes", 0);
  53.         
  54.         SetAlarmTime(pAS + i);
  55.     }
  56. }
  57.  
  58. /*------------------------------------------------
  59.   save settings of Alarm
  60. --------------------------------------------------*/
  61. void SaveAlarm(const PALARMSTRUCT pAS, int count)
  62. {
  63.     int i;
  64.     char subkey[20];
  65.     
  66.     for(i = 0; i < count; i++)
  67.     {
  68.         wsprintf(subkey, "Alarm%d", i + 1);
  69.         
  70.         SetMyRegStr(subkey, "Name", pAS[i].name);
  71.         SetMyRegLong(subkey, "Alarm", pAS[i].bEnable);
  72.         SetMyRegStr(subkey, "Hour", pAS[i].strHours);
  73.         SetMyRegStr(subkey, "Minute", pAS[i].strMinutes);
  74.         SetMyRegStr(subkey, "WDays", pAS[i].strWDays);
  75.         SetMyRegLong(subkey, "Second", pAS[i].second);
  76.         
  77.         SetMyRegStr(subkey, "File", pAS[i].fname);
  78.         SetMyRegLong(subkey, "Hour12", pAS[i].bHour12);
  79.         SetMyRegLong(subkey, "Repeat", pAS[i].bRepeat);
  80.         SetMyRegLong(subkey, "Blink", pAS[i].bBlink);
  81.         SetMyRegLong(subkey, "OnBoot", pAS[i].bBootExec);
  82.         SetMyRegLong(subkey, "Interval", pAS[i].bInterval);
  83.         SetMyRegLong(subkey, "IntervalMinutes", pAS[i].nInterval);
  84.     }
  85. }
  86.  
  87. /*------------------------------------------------
  88.   strHours -> hours, strMinutes -> minutes,
  89.   strWDays -> wdays
  90. --------------------------------------------------*/
  91. void SetAlarmTime(PALARMSTRUCT pAS)
  92. {
  93.     int i;
  94.     
  95.     ParseAlarmFormat(pAS->hours, pAS->strHours, 0, 23);
  96.     for(i = 0; i < 24; i++)
  97.     {
  98.         if(pAS->hours[i]) break;
  99.     }
  100.     if(i == 24)
  101.     {
  102.         for(i = 0; i < 24; i++) pAS->hours[i] = 1;
  103.     }
  104.     
  105.     ParseAlarmFormat(pAS->minutes, pAS->strMinutes, 0, 59);
  106.     
  107.     ParseAlarmFormat(pAS->wdays, pAS->strWDays, 0, 6);
  108.     for(i = 0; i < 7; i++)
  109.     {
  110.         if(pAS->wdays[i]) break;
  111.     }
  112.     if(i == 7)
  113.     {
  114.         for(i = 0; i < 7; i++) pAS->wdays[i] = 1;
  115.     }
  116. }
  117.  
  118. /*------------------------------------------------
  119.    alarm format string -> integer array
  120. --------------------------------------------------*/
  121. void ParseAlarmFormat(char* dst, const char* src, int first, int last)
  122. {
  123.     char part[81];
  124.     int i, j;
  125.     
  126.     for(i = 0; i < last - first + 1; i++)
  127.         dst[i] = 0;
  128.     
  129.     for(i = 0; ; i++)
  130.     {
  131.         if(parse(part, src, i, 81)) break;
  132.         
  133.         if(part[0])
  134.         {
  135.             int n1, n2, n3;
  136.             BOOL asta;
  137.             
  138.             ParseAlarmFormatSub(&n1, &n2, &n3, &asta, part);
  139.             
  140.             if(asta)
  141.             {
  142.                 if(n3 < 0) n3 = 1;
  143.                 for(j = first; j <= last; j += n3)
  144.                     dst[j - first] = 1;
  145.             }
  146.             else if(n1 >= 0 && n2 < 0 && n3 < 0)
  147.             {
  148.                 if(first <= n1 && n1 <= last)
  149.                     dst[n1 - first] = 1;
  150.             }
  151.             else if(n1 >= 0 && n2 >= 0)
  152.             {
  153.                 if(n3 < 0) n3 = 1;
  154.                 j = n1;
  155.                 if(j < first) j = first;
  156.                 for(; j <= n2 && j <= last; j += n3)
  157.                     dst[j - first] = 1;
  158.             }
  159.         }
  160.     }
  161. }
  162.  
  163. /*------------------------------------------------
  164.    "1-2/3" -> n1 = 1, n2 = 2, n3 = 3
  165.    "*"     -> asta = TRUE
  166. --------------------------------------------------*/
  167. void ParseAlarmFormatSub(int *n1, int *n2, int *n3, BOOL *asta,
  168.     const char* part)
  169. {
  170.     const char *p = part;
  171.     
  172.     *n1 = *n2 = *n3 = -1;
  173.     *asta = FALSE;
  174.     
  175.     if(*p == '*')
  176.     {
  177.         *asta = TRUE; p++;
  178.     }
  179.     else
  180.     {
  181.         while(*p)
  182.         {
  183.             if('0' <= *p && *p <= '9')
  184.             {
  185.                 if(*n1 < 0) *n1 = 0;
  186.                 *n1 = *n1 * 10 + *p - '0';
  187.             }
  188.             else break;
  189.             p++;
  190.         }
  191.         
  192.         if(*p == '-') p++;
  193.         else return;
  194.         
  195.         while(*p)
  196.         {
  197.             if('0' <= *p && *p <= '9')
  198.             {
  199.                 if(*n2 < 0) *n2 = 0;
  200.                 *n2 = *n2 * 10 + *p - '0';
  201.             }
  202.             else break;
  203.             p++;
  204.         }
  205.     }
  206.     
  207.     if(*p == '/') p++;
  208.     else return;
  209.     
  210.     while(*p)
  211.     {
  212.         if('0' <= *p && *p <= '9')
  213.         {
  214.             if(*n3 < 0) *n3 = 0;
  215.             *n3 = *n3 * 10 + *p - '0';
  216.         }
  217.         else break;
  218.         p++;
  219.     }
  220. }
  221.